//
//  BRGuideVC.m
//  standard
//
//  Created by 甘卫国 on 2025/1/17.
//

#import "BRGuideVC.h"
#import "AppDelegate.h"

#import "BRAlertCustomView.h"

@interface BRGuideVC ()

@property (nonatomic,strong) QMUIButton *btn;

@property (nonatomic,strong) QMUILabel *textL;

@property (nonatomic,strong) BRLanguageBtn *languageBtn;

@end

@implementation BRGuideVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 修复：启用 UI 设置
    [self setUI];
}


- (void)goStarted {
    
    [BRGuideVC br_setFirstIn];
    
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [app didInitWindow];
}


- (void)showLanguageSet {
    
    BRAlertCustomView *alert = [[BRAlertCustomView alloc] initWithFrame:self.view.bounds type:1];
    alert.titleStr = Localized(@"set_selectLanguage");
    alert.listArr = [[BRLanguageManager share] languageDescribes];
    [self.view addSubview:alert];
    [alert selectStr:[[BRLanguageManager share] describe]];
    WS(weakSelf);
    alert.alertBlock = ^(NSString * _Nonnull changeText, NSInteger index, void (^ _Nonnull hidd)(BOOL)) {
        
        NSArray *laArr  = [[BRLanguageManager share] languages];
        if (index >= laArr.count) hidd(NO);
        BRLanguageModel *model = laArr[index];
        [[BRLanguageManager share] setNewLanguage:model.language changeNew:NO];
        [weakSelf br_updateLanguageText];
        hidd(YES);
    };
    
  
}

- (void)br_updateLanguageText {
    
    self.languageBtn.titleL.text = [BRLanguageManager share].describe;
    
    
    self.textL.text = Localized(@"guide_text");
    [self.btn setTitle:Localized(@"guide_goStarted") forState:UIControlStateNormal];
    
}



#pragma mark - UI --
- (void)setUI {
    
    
    self.view.backgroundColor = UIColorMakeWithHex(@"#e4943c");
    
    [self.view addSubview:self.languageBtn];
    [self.languageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(4 + KStatusBarHeight);
        make.centerX.offset(0);
        
    }];
    WS(weakSelf);
    self.languageBtn.toChangeLanguageBlcok = ^{
        [weakSelf showLanguageSet];
    };
    
    CGFloat h = kScale(385);
    UIImageView *img = [[UIImageView alloc] initWithImage:UIImageMake(@"guide_pic")];
    [self.view addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.centerY.offset(0);
        make.left.offset(8);
        make.right.offset(-8);
        make.height.mas_equalTo(h);
    }];
 
    
    QMUIButton *goBtn = [BRToolsView br_btnBGImg:Localized(@"guide_goStarted")];
    [self.view addSubview:goBtn];
    [goBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(52);
        make.width.mas_equalTo(kScreenWidth - 28);
        make.left.equalTo(self.view).offset(14);
        make.bottom.equalTo(self.view.mas_bottom).offset(- (15 + kSafeAreaBottomHeight));
    }];
    
    [goBtn addTarget:self action:@selector(goStarted) forControlEvents:UIControlEventTouchUpInside];
    
    
    QMUILabel *textL = [BRToolsView br_label:Localized(@"guide_text") textColor:UIColor.whiteColor font:[UIFont br_fontB:20] textAlignment:NSTextAlignmentCenter];
    textL.numberOfLines = 0;
    [self.view addSubview:textL];
    [textL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(28);
        make.right.offset(-28);
        make.bottom.equalTo(goBtn.mas_top).offset(-28);
    }];
    
    _btn = goBtn;
    _textL = textL;
}

- (BRLanguageBtn *)languageBtn {
    
    if (_languageBtn == nil) {
        _languageBtn = [[BRLanguageBtn alloc] init];
    }
    return _languageBtn;
}

+ (BOOL)isFirstInApp {
    if ([[NSUserDefaults standardUserDefaults] objectForKey:kFirstInApp]) {
        return [[NSUserDefaults standardUserDefaults] boolForKey:kFirstInApp];
    }
    return NO;
}

+ (void)br_setFirstIn {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kFirstInApp];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


@end


@implementation BRLanguageBtn

- (instancetype)init
{
    self = [super init];
    if (self) {
        // 修复：启用 UI 设置
        [self setUI];
    }
    return self;
}

- (void)toChangeLanguage {
    if (self.toChangeLanguageBlcok) {
        self.toChangeLanguageBlcok();
    }
}


#pragma mark - UI --
- (void)setUI {
    
    self.layer.cornerRadius = 18;
    self.layer.masksToBounds = YES;
    
    self.backgroundColor = kColorShadow(0.38);
    
    UIImageView *img = [[UIImageView alloc] initWithImage:UIImageMake(@"arrow_down_gray36")];
    
    [self addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(36);
        make.top.right.bottom.offset(0);
    }];
    
    self.titleL = [BRToolsView br_label:[BRLanguageManager share].describe textColor:UIColor.whiteColor font:[UIFont br_fontS:14]];
    [self addSubview:self.titleL];
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(21);
        make.right.offset(-36);
        make.centerY.offset(0);
        make.width.mas_greaterThanOrEqualTo(68);
    }];
    
    UIControl *btn = [[UIControl alloc] init];
    [self addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    [btn addTarget:self action:@selector(toChangeLanguage) forControlEvents:UIControlEventTouchUpInside];
}

@end
